package com.rr.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.rr.springcloud.cilent.PaymentClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author RR
 * @date 2021/3/27 21:51
 */
@RestController
@DefaultProperties(defaultFallback = "globalFallBack")
public class OrderController {
    @Autowired
    private PaymentClient paymentClient;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id){
        return paymentClient.paymentInfoOK(id);
    }

//    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutFallBack",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
//    })
    @HystrixCommand
    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfoTimeout(@PathVariable("id") Integer id) {
        //报错或超过2秒未响应或报错都会执行兜底方法
        int i = 1/0;
        return paymentClient.paymentInfoTimeout(id);
    }

    public String paymentInfoTimeoutFallBack(Integer id) {
        return "系统1秒内未响应或报错，超时兜底"+"线程池：" + Thread.currentThread().getName() + ",id:" + id;
    }

    public String globalFallBack(){
        return "执行全局服务降级方法";
    }

}
